package com.weiqiao.mall.service.impl;

import com.google.gson.Gson;
import com.mingsoft.util.StringUtil;
import com.weiqiao.mall.domain.BankEntity;
import com.weiqiao.mall.domain.RelationBankCardEntity;
import com.weiqiao.mall.domain.RelationBankEntity;
import com.weiqiao.mall.repository.IBankRepository;
import com.weiqiao.mall.service.IBankService;
import com.weiqiao.mall.service.vo.BankVO;
import com.weiqiao.utils.DateUtils;
import com.weiqiao.utils.RedisUtil;
import com.weiqiao.utils.SecurityUtils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.lang.StringUtils;

import org.bson.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service("BankServiceImpl")
public class BankServiceImpl implements IBankService {

    @Autowired
    private IBankRepository bank;

    @Override
    public List<BankEntity> hotBankList(BankVO vo){
        //Bson filter, Bson sort, int skip, int limit, int company_id
        Document doc = new Document(); // 查询条件
        Document sort = new Document(); // 排序
        doc.append("delete_flag",0);
        if(!StringUtils.isBlank(vo.getName())){
            doc.append("name",new Document("$regex",vo.getName()));
        }
        sort.append("create_date", -1);
        int pagesize = vo.getSize(); // 多少个
        int pagenum = vo.getPage() * vo.getSize(); // 页数
        int company_id = vo.getCompany_id(); // 公司id
        List<String> list = bank.hotBankList(doc,sort,pagenum,pagesize,company_id);
        List<BankEntity> result = new ArrayList<>();
        SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
        Gson gson = new Gson();
        list.forEach(s -> {
            BankEntity bankEntity = gson.fromJson(s,BankEntity.class);
            String time = bankEntity.getCreate_date();
            long create_time = Long.parseLong(time);
            bankEntity.setCreate_date(sd.format(new Date(create_time)));
            result.add(bankEntity);
        });
        return  result;
    }

    @Override
    public BankEntity getBankInfo(int companyId, String bank_id) {
        String info = bank.getBankInfo(companyId, new Document("bank_id", bank_id));
        if (StringUtil.isBlank(info)) new BankEntity();
        return new Gson().fromJson(info, BankEntity.class);    }

    @Override
    public Map<String, Object> updateBank(BankVO vo){
        Document bson = new Document(); //条件
        Document doc = new Document(); // 内容
        Map<String,Object> map = new HashedMap();
        if(StringUtils.isBlank(vo.getBank_id())){
            map.put("result",false);
            map.put("resultmsg", "银行Id不能为空");
            return map;
        }
        bson.append("bank_id",vo.getBank_id());
        int company_id = vo.getCompany_id();
        String time = DateUtils.currentTimeMillis();
        doc.append("update_date",time);
        doc.append("name",vo.getName());
        doc.append("logo_url",vo.getLogo_url());
        doc.append("label",vo.getLabel());
        doc.append("title",vo.getTitle());
        doc.append("background_color",vo.getBackground_color());
        doc.append("settlement_rules",vo.getSettlement_rules());
        doc.append("course_url",vo.getCourse_url());
        doc.append("activity_details",vo.getActivity_details());
        Boolean lean = bank.updateBank(bson ,doc ,company_id);
        if(lean){
            map.put("result",true);
            map.put("resultmsg", "修改成功");
        }else{
            map.put("result",false);
            map.put("resultmsg", "修改失败");
        }
        return  map;
    }

    @Override
    public Map<String, Object> insertBank(BankEntity vo){
        int company_id = SecurityUtils.getCompanyId();
        Map<String,Object> map = new HashedMap();
        String time = DateUtils.currentTimeMillis();
        vo.setCreate_date(time);
        vo.setBank_id(time);
        String json = new Gson().toJson(vo);
        Boolean lean = bank.insertBank(json,company_id);
        if(lean){
            map.put("result",true);
            map.put("resultmsg", "添加成功");
        }else{
            map.put("result",false);
            map.put("resultmsg", "添加失败");
        }
        return  map;
    }

    @Override
    public Map<String, Object> deleteBank(BankVO vo){
        int company_id = vo.getCompany_id();
        Map<String,Object> map = new HashedMap();
        Document filter = new Document();
        Document doc = new Document();
        if(StringUtils.isBlank(vo.getBank_id())){
            map.put("result",false);
            map.put("resultmsg", "银行Id不能为空");
            return  map;
        }
        filter.append("bank_id",vo.getBank_id());
        doc.append("delete_flag",1);
        Boolean lean = bank.deleteBank(filter,doc,company_id);
        if(lean){
            map.put("result",true);
            map.put("resultmsg", "删除成功");
        }else{
            map.put("result",false);
            map.put("resultmsg", "删除失败");
        }
        return  map;
    }

    @Override
    public int countBank(BankVO vo){
        Document doc = new Document();
        int company_id = vo.getCompany_id();
        doc.append("delete_flag", 0);
        return  bank.countBank(company_id ,doc);
    }

    @Override
    public List<RelationBankEntity.BankInfoEntity>  getRelationBankInfo() {
        List<RelationBankEntity.BankInfoEntity> list = new ArrayList<>();
        Jedis jedis = RedisUtil.getJedis();
        try {
            String info = jedis.get("relation_bank_info");
            if (!StringUtil.isBlank(info)) {
                RelationBankEntity entity = new Gson().fromJson(info, RelationBankEntity.class);
                if (!StringUtil.isBlank(entity) && !CollectionUtils.isEmpty(entity.getData())) {
                    list = entity.getData();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            RedisUtil.returnResource(jedis);
        }
        return list;
    }

    @Override
    public List<RelationBankCardEntity.CardInfoEntity> relationBankCardList(String code) {
        List<RelationBankCardEntity.CardInfoEntity> list = new ArrayList<>();
        Jedis jedis = RedisUtil.getJedis();
        try {
            String info = jedis.get("relation_bank_card_info_" + code);
            if (!StringUtil.isBlank(info)) {
                RelationBankCardEntity entity = new Gson().fromJson(info, RelationBankCardEntity.class);
                if (!StringUtil.isBlank(entity) && !CollectionUtils.isEmpty(entity.getData())) {
                    list = entity.getData();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            RedisUtil.returnResource(jedis);
        }
        return list;
    }

    @Override
    public List<BankEntity> getBankList() {
        int companyId = SecurityUtils.getCompanyId();
        Document doc = new Document("delete_flag", 0); // 查询条件
        List<String> list = bank.getAll(companyId, doc);
        List<BankEntity> result = new ArrayList<>();
        Gson gson = new Gson();
        list.forEach(s -> {
            BankEntity bankEntity = gson.fromJson(s,BankEntity.class);
            result.add(bankEntity);
        });
        return result;
    }
}
